<template>
  <div class="login_container">
    <el-row>
      <el-col :span="0" :md="12"></el-col>
      <el-col :span="24" :md="12">
        <el-form
          class="login_form"
          :model="loginForm"
          :rules="rules"
          ref="loginForms"
        >
          <h1>Hello</h1>
          <h2>欢迎来到小何很牛超级系统</h2>
          <el-form-item prop="username">
            <el-input
              class="login_input"
              v-model="loginForm.username"
              :prefix-icon="User"
            />
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              class="login_input"
              v-model="loginForm.password"
              :prefix-icon="Lock"
              type="password"
              show-password
            />
          </el-form-item>
          <el-form-item>
            <el-button
              @click="login"
              class="login_bnt"
              :loading="loading"
              type="primary"
              size="default"
            >
              登陆
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { User, Lock } from "@element-plus/icons-vue";
import { reactive, ref } from "vue";
import { ElNotification } from "element-plus";
// 引入获取当前为 早上|上午|下午|晚上 的函数
import { getTime } from "@/utils/time";
// 引入防抖函数
import { debounce } from "@/utils/debounce";
// 引入并获取路由器与路由
import { useRouter, useRoute } from "vue-router";
let $router = useRouter();
let $route = useRoute();
// 引入用户相关仓库
import useUserStore from "@/store/modules/user";
let useStore = useUserStore();

// 获取el-form表单元素
let loginForms = ref();

// 收集账号与密码的数据
let loginForm = reactive({
  username: "admin",
  password: "atguigu123",
});

// 定义变量控制加载效果
let loading = ref(false);

// 登陆
const login = debounce(async () => {
  loading.value = true;
  // 表单校验
  // validate:对整个表单的内容进行验证。 接收一个回调函数，或返回 Promise
  await loginForms.value.validate(async (valid: any) => {
    if (valid) {
      try {
        // 登陆成功
        await useStore.userLogin(loginForm);
        let redirect: any = $route.query.redirect;
        $router.push({ path: redirect || "/" });
        // 登陆成功的提示信息
        ElNotification({
          type: "success",
          message: "登陆成功",
          title: `Hi,${getTime()}好！`,
        });
      } catch (error) {
        // 登陆失败的提示信息
        ElNotification({
          type: "error",
          message: (error as Error).message,
        });
      }
      loading.value = false;
    } else {
      loading.value = false;
    }
  });
}, 500);

const rules = {
  username: [
    { required: true, message: "用户名不能为空！", trigger: "blur" },
    {
      required: true,
      min: 5,
      max: 8,
      message: "用户名输入至少五位，最多八位！",
      trigger: ["change", "blur"],
    },
  ],
  password: [
    { required: true, message: "密码不能为空！", trigger: "blur" },
    {
      required: true,
      min: 6,
      max: 10,
      message: "密码输入至少六位，最多十位！",
      trigger: ["change", "blur"],
    },
  ],
};
</script>

<style scoped lang="scss">
.login_container {
  width: 100%;
  height: 100vh;
  background: url("@/assets/images/background.jpg") no-repeat;
  background-size: cover;

  .login_form {
    position: relative;
    margin: 0 15%;
    top: 30vh;
    background: url("@/assets/images/login_form.png") no-repeat;
    background-size: cover;
    padding: 40px;

    h1 {
      color: white;
      font-size: 40px;
    }

    h2 {
      font-size: 20px;
      color: white;
      margin: 20px 0;
    }

    .login_bnt {
      width: 100%;
      margin: 0 48px;
    }

    .login_input {
      margin: 0 48px;
    }
  }
}

:deep(.el-form-item__error) {
  margin: 0 48px;
}

@media screen and (max-width: 768px) {
  .login_form {
    margin: 0 16px !important;
    padding: 32px !important;

    .login_input,
    .login_bnt {
      margin: 0 !important;
    }

    h1 {
      font-size: 32px !important;
    }

    h2 {
      font-size: 14px !important;
      margin: 20px 0;
    }
  }

  :deep(.el-form-item__error) {
    margin: 0 0px;
  }
}
</style>
